package programmierung.aufgabe4;

public class EWN {
	
	public void validateToken(String token) throws IllegalTokenException {
		String[] sToken;
		
		if(token != null) {
			sToken = token.split(" ");
			if(sToken.length == 4) {
				checkToken(sToken);
			} else {
				throw new IllegalTokenException();
			}
		} else {
			throw new IllegalTokenException();
		}
	}
	
	private void checkToken(String[] sToken) throws IllegalTokenException {
		
		for(int i = 0; i < sToken.length; i++) {
			switch(i) {
			case 0: //Farbe
				if(!(sToken[0].contentEquals("RED") || sToken[0].contentEquals("BLUE"))) {
					throw new IllegalTokenException("Falsche Farbe");
				}
				break;
			case 1: //Nummer
				if(!(sToken[1].contentEquals("ONE") ||
					  sToken[1].contentEquals("TWO") ||
					  sToken[1].contentEquals("THREE") ||
					  sToken[1].contentEquals("FOUR") ||
					  sToken[1].contentEquals("FIVE") ||
					  sToken[1].contentEquals("SIX") )) {
					throw new IllegalTokenException("Falsche Nummer");
				}
				break;
			case 2: //x Koordinate
				try {
					Integer.valueOf(sToken[2]);
				} catch(NumberFormatException e) {
					throw new IllegalTokenException("Falsche Koordinaten");
				}
				break;
			case 3: //y Koordinate
				try {
					Integer.valueOf(sToken[2]);
				} catch(NumberFormatException e) {
					throw new IllegalTokenException("Falsche Koordinaten");
				}
			}
		}
		
	}
	
	public void validateMove(String move) throws IllegalMoveException {
		String[] sMove;
		
		if(move != null) {
			sMove = move.split(" ");
			if(sMove.length == 3) {
				checkMove(sMove);
			} else {
				throw new IllegalMoveException();
			}
		} else {
			throw new IllegalMoveException();
		}
	}
	
	private void checkMove(String[] sMove) throws IllegalMoveException {
		
		for(int i = 0; i < sMove.length; i++) {
			switch(i) {
			case 0: //Farbe
				if(!(sMove[0].contentEquals("RED") || sMove[0].contentEquals("BLUE"))) {
					throw new IllegalMoveException("Falsche Farbe");
				}
				break;
			case 1: //Nummer
				if(!(sMove[1].contentEquals("ONE") ||
					  sMove[1].contentEquals("TWO") ||
					  sMove[1].contentEquals("THREE") ||
					  sMove[1].contentEquals("FOUR") ||
					  sMove[1].contentEquals("FIVE") ||
					  sMove[1].contentEquals("SIX") )) {
					throw new IllegalMoveException("Falsche Nummer");
				}
				break;
			case 2: //Richtung
				if(!(sMove[2].contentEquals("DIAGONAL") ||
						  sMove[2].contentEquals("HORIZONTAL") ||
						  sMove[2].contentEquals("VERTICAL") )) {
						throw new IllegalMoveException("Falsche Richtung");
					}
				break;
			}
		}
	}

	public class IllegalTokenException extends Exception {
		/**
		 * 
		 */
		private static final long serialVersionUID = 8478137698319839811L;
		public IllegalTokenException() {
			super();
		}
		public IllegalTokenException(String message) {
			super(message);
		}
		public IllegalTokenException(String message, Throwable cause) {
			super(message, cause);
		}
		public IllegalTokenException(Throwable cause) {
			super(cause);
		}
	}
	
	public class IllegalMoveException extends Exception {
		/**
		 * 
		 */
		private static final long serialVersionUID = 4842560322780298930L;
		public IllegalMoveException() {
			super();
		}
		public IllegalMoveException(String message) {
			super(message);
		}
		public IllegalMoveException(String message, Throwable cause) {
			super(message, cause);
		}
		public IllegalMoveException(Throwable cause) {
			super(cause);
		}
	}
}
